/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.forces.gravity.potential;

import ore.errors.OrekitException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * EGM Format reader used by {@link PotentialReaderFactory}
 *
 * @see ore.forces.gravity.potential.PotentialReaderFactory
 * 
 * @author Fabien Maussion
 */
public class EGMFormatReader
    extends PotentialCoefficientsReader
{


    public EGMFormatReader(final String supportedNames) {
        super(supportedNames);
        this.ae = 6378136.3;
        this.mu = 398600.4415e9;
    }


    /** {@inheritDoc}
     */
    public void loadData(InputStream input, String name)
        throws IOException, ParseException, OrekitException
    {
        final BufferedReader r = new BufferedReader(new InputStreamReader(input));
        final List<double[]> cl = new ArrayList<double[]>();
        final List<double[]> sl = new ArrayList<double[]>();
        boolean okFields = true;
        for (String line = r.readLine(); okFields && line != null; line = r.readLine()) {
            if (line.length() >= 15) {
                /*
                 * Get the fields defining the current the potential terms
                 */
                final String[] tab = line.trim().split("\\s+");
                if (tab.length != 6) {
                    okFields = false;
                }

                final int i = Integer.parseInt(tab[0]);
                final int j = Integer.parseInt(tab[1]);
                final double c = Double.parseDouble(tab[2]);
                final double s = Double.parseDouble(tab[3]);
                {
                    /*
                     * Extend the cl array if needed
                     */
                    int ck = cl.size();
                    for (int k = ck; k <= i; ++k) {
                        cl.add(new double[k + 1]);
                    }
                }
                double[] cli = cl.get(i);
                {
                    /*
                     * Extend the sl array if needed
                     */
                    int sk = sl.size();
                    for (int k = sk; k <= i; ++k) {
                        sl.add(new double[k + 1]);
                    }
                }
                double[] sli = sl.get(i);

                cli[j] = c;
                sli[j] = s;
            }
        }
        /*
         */
        if ((!okFields) || cl.size() < 1) {
            throw new OrekitException("the reader is not adapted to the format ({0})",
                                      name);
        }
        else {
            /*
             * Convert to simple triangular arrays
             */
            this.normalizedC = cl.toArray(new double[cl.size()][]);
            this.normalizedS = sl.toArray(new double[sl.size()][]);
            this.readCompleted = true;
        }
    }

}
